<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息 - 教学管理系统</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 0;
        }

        .container {
            display: flex;
            min-height: 100vh;
        }

        .sidebar {
            width: 250px;
            background-color: #2c3e50;
            color: white;
            padding-top: 20px;
        }

        .sidebar-header {
            padding: 0 20px 20px;
            border-bottom: 1px solid #3c5166;
        }

        .sidebar-header h2 {
            margin: 0;
            font-size: 20px;
        }

        .user-info {
            margin-top: 10px;
            font-size: 14px;
            opacity: 0.8;
        }

        .menu-section {
            padding: 10px 20px;
            font-size: 14px;
            color: rgba(255, 255, 255, 0.6);
            border-bottom: 1px solid #3c5166;
            margin-top: 15px;
        }

        .menu {
            list-style: none;
            padding: 0;
            margin: 20px 0;
        }

        .menu li {
            padding: 12px 20px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .menu li:hover {
            background-color: #3c5166;
        }

        .menu li.active {
            background-color: #3498db;
        }

        .menu a {
            color: white;
            text-decoration: none;
            display: block;
        }

        .content {
            flex: 1;
            padding: 20px;
        }

        .content-header {
            margin-bottom: 30px;
        }

        .content-header h1 {
            margin: 0;
            color: #333;
            font-size: 24px;
        }

        .profile-container {
            background-color: white;
            border-radius: 8px;
            padding: 30px;
            margin-top: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            max-width: 600px;
        }

        .profile-section {
            margin-bottom: 30px;
        }

        .profile-section h2 {
            color: #333;
            font-size: 18px;
            margin-top: 0;
            margin-bottom: 20px;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }

        .form-group input {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .form-actions {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 20px;
        }

        .btn-cancel,
        .btn-save {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .btn-cancel {
            background-color: #f1f1f1;
            color: #333;
        }

        .btn-save {
            background-color: #2ecc71;
            color: white;
        }

        .alert {
            padding: 10px 15px;
            border-radius: 4px;
            margin-bottom: 15px;
            display: none;
        }

        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-danger {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .logout {
            margin-top: auto;
            padding: 20px;
        }

        .logout a {
            color: #e74c3c;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>教学管理系统</h2>
                <div class="user-info" th:if="${session.user}">
                    <span th:text="${session.user.username}">admin1</span> - 管理员
                </div>
            </div>
            <div class="menu-section">基础管理</div>
            <ul class="menu">
                <li><a href="/admin/dashboard">系统概览</a></li>
                <li><a href="/admin/teachers">教师管理</a></li>
                <li><a href="/admin/students">学生管理</a></li>
                <li><a href="/admin/courses">课程管理</a></li>
                <li><a href="/admin/classrooms">教室管理</a></li>
            </ul>

            <div class="menu-section">教务管理</div>
            <ul class="menu">
                <li><a href="/admin/course-selections">选课管理</a></li>
                <li><a href="/admin/grades">成绩管理</a></li>
            </ul>

            <div class="menu-section">系统设置</div>
            <ul class="menu">
                <li class="active"><a href="/admin/profile">个人信息</a></li>
            </ul>

            <div class="logout">
                <a href="/logout">退出登录</a>
            </div>
        </div>

        <div class="content">
            <div class="content-header">
                <h1>个人信息</h1>
            </div>

            <div class="profile-container">
                <div id="profile-alert-success" class="alert alert-success">操作成功</div>
                <div id="profile-alert-danger" class="alert alert-danger">操作失败</div>

                <div class="profile-section">
                    <h2>基本信息</h2>
                    <div class="form-group">
                        <label for="userId">用户ID</label>
                        <input type="text" id="userId" name="userId" readonly>
                    </div>
                    <div class="form-group">
                        <label for="role">角色</label>
                        <input type="text" id="role" name="role" readonly>
                    </div>
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" id="username" name="username">
                    </div>
                    <div class="form-actions">
                        <button class="btn-save" onclick="updateProfile()">保存修改</button>
                    </div>
                </div>

                <div class="profile-section">
                    <h2>修改密码</h2>
                    <div class="form-group">
                        <label for="oldPassword">原密码</label>
                        <input type="password" id="oldPassword" name="oldPassword">
                    </div>
                    <div class="form-group">
                        <label for="newPassword">新密码</label>
                        <input type="password" id="newPassword" name="newPassword">
                    </div>
                    <div class="form-group">
                        <label for="confirmPassword">确认新密码</label>
                        <input type="password" id="confirmPassword" name="confirmPassword">
                    </div>
                    <div class="form-actions">
                        <button class="btn-save" onclick="changePassword()">修改密码</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 页面加载时获取个人信息
        document.addEventListener('DOMContentLoaded', function () {
            fetchProfile();
        });

        // 获取个人信息
        function fetchProfile() {
            fetch('/api/admin/profile')
                .then(response => {
                    if (!response.ok) {
                        if (response.status === 401) {
                            alert('未授权，请重新登录');
                            window.location.href = '/login';
                        }
                        throw new Error('获取个人信息失败: ' + response.statusText);
                    }
                    return response.json();
                })
                .then(data => {
                    document.getElementById('userId').value = data.userId;
                    document.getElementById('role').value = data.role;
                    document.getElementById('username').value = data.username;
                })
                .catch(error => {
                    console.error('获取个人信息出错:', error);
                    showAlert('danger', '获取个人信息失败');
                });
        }

        // 更新个人信息
        function updateProfile() {
            const userId = document.getElementById('userId').value;
            const username = document.getElementById('username').value.trim();

            if (!username) {
                showAlert('danger', '用户名不能为空');
                return;
            }

            const profileData = {
                userId: parseInt(userId),
                username: username
            };

            fetch('/api/admin/profile', {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(profileData)
            })
                .then(response => {
                    if (!response.ok) {
                        if (response.status === 401) {
                            alert('未授权，请重新登录');
                            window.location.href = '/login';
                        }
                        return response.text().then(text => { throw new Error(text || '更新个人信息失败') });
                    }
                    return response.json();
                })
                .then(data => {
                    showAlert('success', '个人信息更新成功');
                    // 更新DOM中显示的用户名
                    const userInfoSpan = document.querySelector('.user-info span');
                    if (userInfoSpan) {
                        userInfoSpan.textContent = data.username;
                    }
                })
                .catch(error => {
                    console.error('更新个人信息出错:', error);
                    showAlert('danger', '更新个人信息失败: ' + error.message);
                });
        }

        // 修改密码
        function changePassword() {
            const oldPassword = document.getElementById('oldPassword').value;
            const newPassword = document.getElementById('newPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (!oldPassword || !newPassword || !confirmPassword) {
                showAlert('danger', '请填写所有密码字段');
                return;
            }

            if (newPassword !== confirmPassword) {
                showAlert('danger', '新密码和确认密码不一致');
                return;
            }

            if (newPassword.length < 6) {
                showAlert('danger', '新密码长度不能少于6个字符');
                return;
            }

            const passwordData = {
                oldPassword: oldPassword,
                newPassword: newPassword
            };

            fetch('/api/admin/profile/password', {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(passwordData)
            })
                .then(response => {
                    if (!response.ok) {
                        if (response.status === 401) {
                            alert('未授权，请重新登录');
                            window.location.href = '/login';
                        } else if (response.status === 400) {
                            return response.text().then(text => { throw new Error(text || '原密码不正确') });
                        }
                        return response.text().then(text => { throw new Error(text || '修改密码失败') });
                    }
                    return response.text();
                })
                .then(() => {
                    showAlert('success', '密码修改成功');
                    // 清空密码字段
                    document.getElementById('oldPassword').value = '';
                    document.getElementById('newPassword').value = '';
                    document.getElementById('confirmPassword').value = '';
                })
                .catch(error => {
                    console.error('修改密码出错:', error);
                    showAlert('danger', '修改密码失败: ' + error.message);
                });
        }

        // 显示提示信息
        function showAlert(type, message) {
            const successAlert = document.getElementById('profile-alert-success');
            const dangerAlert = document.getElementById('profile-alert-danger');

            // 隐藏所有提示
            successAlert.style.display = 'none';
            dangerAlert.style.display = 'none';

            // 显示相应类型的提示
            if (type === 'success') {
                successAlert.textContent = message;
                successAlert.style.display = 'block';
            } else if (type === 'danger') {
                dangerAlert.textContent = message;
                dangerAlert.style.display = 'block';
            }

            // 3秒后自动隐藏
            setTimeout(() => {
                if (type === 'success') {
                    successAlert.style.display = 'none';
                } else if (type === 'danger') {
                    dangerAlert.style.display = 'none';
                }
            }, 3000);
        }
    </script>
</body>

</html>